package org.bb.initdata.command;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.bb.initdata.model.Product;
import org.bb.initdata.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GetData implements CommandLineRunner {

    private final ProductService productService;

    public GetData(ProductService productService) {
        this.productService = productService;
    }

    @Override
    public void run(String... args) throws Exception {
        // productService.remove(Wrappers.lambdaQuery());
        // productService.init("https://www.chinadbs.com/zc/18_p{}.html", 1);
        // 查询所有的数据，设置 price 后保存
        List<Product> list = productService.list();
        list.forEach(product -> product.setPrice(
                RandomUtil.randomBigDecimal(
                        BigDecimal.valueOf(666l),
                        BigDecimal.valueOf(10000l)
                ))
        );
        productService.saveOrUpdateBatch(list);
    }
}
